package com.itboye.shop.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.util.helper.utils.TimeCount;
import com.itboye.shop.util.loadutil.MAlert;

import java.util.Observable;
import java.util.Observer;

/**
 * 忘记密码2
 */
public class ForgetPwdTwoActivity extends BaseActivity implements Observer {
    ImageView back;
    TextView tvTitle;
    Button btn_ok;
    EditText edit_newpwd,edit_confirmpwd,forget02_yzm;
    TextView forget02_phone;//手机号
    TextView txt_sendyzm;//获取验证码
    String phone;
    private TimeCount time;// 倒计时

    UserPresenter userPresenter;

    public static String UPDATE_PASS = "update.ok";// 修改密码成功的指令

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_forget_pass_two);
        tvTitle.setText("忘记密码");
        phone=getIntent().getStringExtra("phone");

        userPresenter=new UserPresenter(this);

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

            case R.id.txt_sendyzm://获取验证码
                userPresenter.sendVerificationCode("+86", phone, 2 + "");

                time = new TimeCount(60000, 1000, txt_sendyzm);// 构造CountDownTimer对象
                time.start();

                break;
            case R.id.btn_ok:
                String pass01=edit_newpwd.getText().toString();
                String pass02=edit_confirmpwd.getText().toString();
                String yzm=forget02_yzm.getText().toString();
                if(pass01.equals("")){
                    MAlert.alert("请输入密码",Gravity.CENTER);
                    return;
                }
                if(pass02.equals("")){
                    MAlert.alert("请输入密码",Gravity.CENTER);
                    return;
                }
                if(yzm.equals("")){
                    MAlert.alert("请输入验证码",Gravity.CENTER);
                    return;
                }
                if(!pass01.equals(pass02)){
                    MAlert.alert("两次密码输入不一致",Gravity.CENTER);
                    return;
                }
                userPresenter.YanZhengverificationCode("+86",phone,yzm,"2");

                break;

        }
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity resultEntity=handlerError(data);
        if (resultEntity!=null) {
            if (resultEntity.getEventType()== UserPresenter.send_code_success) {//发送验证码
                forget02_phone.setText("验证码已发送到手机"+phone);
                MAlert.alert(resultEntity.getData(), Gravity.CENTER);
            }
            if(resultEntity.getEventType()== UserPresenter.send_code_success) {
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
            if (resultEntity.getEventType()== UserPresenter.yanzheng_code_success) {//验证验证码
                showProgressDialog("请稍后",true);
                userPresenter.updatePass("+86",forget02_yzm.getText().toString(),phone,edit_newpwd.getText().toString());
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
            if(resultEntity.getEventType()== UserPresenter.yanzheng_code_success) {
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
            if (resultEntity.getEventType()== UserPresenter.updata_pass_success) {//修改密码
                MAlert.alert(resultEntity.getData(), Gravity.CENTER);
                finish();
                Intent intent=new Intent(UPDATE_PASS);
                sendBroadcast(intent);

            }
            if(resultEntity.getEventType()== UserPresenter.updata_pass_success) {
                MAlert.alert(resultEntity.getData(),Gravity.CENTER);
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }

}
